#!/usr/bin/env python
# coding=utf-8
import thriftpy2
from datetime import datetime
pingpong_thrift = thriftpy2.load("pingpong.thrift", module_name="pingpong_thrift")
from qp_dstore.data_store import DataStore
from thriftpy2.rpc import make_server
import msgpack
import pickle
import zlib


instance = DataStore.get_instance()

class Dispatcher(object):
    def query(self, request):
        # 调用对象的方法
        
        # request = pingpong_thrift.St_Query_Req()
        params = msgpack.unpackb(request.params, encoding="utf8")
        params["start_dt"] = datetime.strptime(params["start_dt"], "%Y-%m-%d %H:%M:%S")
        params["end_dt"] = datetime.strptime(params["end_dt"], "%Y-%m-%d %H:%M:%S")
        msg = None
        try:
            data = getattr(instance, request.method_name)(**params)
            # 把DataFrame转换为pickle字符串
            pk_data = pickle.dumps(data)
            # 把pickle字符串进行压缩
            pd_data = zlib.compress(pk_data)
        except Exception as e:
            pd_data = None
            msg = e
        response = pingpong_thrift.St_Query_Rsp
        response.status = 1 if pd_data is not None else 0
        response.msg = pd_data
        response.error = msg if msg else None
        return response


    


server = make_server(pingpong_thrift.PingPong, Dispatcher(), '127.0.0.1', 6000)

server.serve()


